package com.keba.lamp.complaint.service.impl;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.collection.CollectionUtil;
import com.google.common.collect.Lists;
import com.keba.basic.base.service.impl.SuperServiceImpl;
import com.keba.basic.context.ContextUtil;
import com.keba.basic.utils.ArgumentAssert;
import com.keba.lamp.common.constant.AppendixType;
import com.keba.lamp.common.constant.BizConstant;
import com.keba.lamp.complaint.entity.Case;
import com.keba.lamp.complaint.entity.ComFileRel;
import com.keba.lamp.complaint.entity.EvidenceCaseRel;
import com.keba.lamp.complaint.manager.ComFileRelManager;
import com.keba.lamp.complaint.manager.EvidenceCaseRelManager;
import com.keba.lamp.complaint.service.EvidenceCaseRelService;
import com.keba.lamp.complaint.vo.result.EvidenceCaseRelResultVO;
import com.keba.lamp.complaint.vo.save.EvidenceVO;
import com.keba.lamp.complaint.vo.update.EvidenceCaseRelUpdateSummarizeVO;
import com.keba.lamp.file.entity.File;
import com.keba.lamp.file.service.AppendixService;
import com.keba.lamp.file.service.FileService;
import com.keba.lamp.model.vo.save.AppendixSaveVO;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang.StringUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.List;
import java.util.Objects;

/**
 * <p>
 * 业务实现类
 * 证据目录和案件关联表
 * </p>
 *
 * @author chaowu.wang
 * @date 2025-04-02 00:12:46
 * @create [2025-04-02 00:12:46] [chaowu.wang] [代码生成器生成]
 */
@Slf4j
@RequiredArgsConstructor
@Service
public class EvidenceCaseRelServiceImpl extends SuperServiceImpl<EvidenceCaseRelManager, Long, EvidenceCaseRel> implements EvidenceCaseRelService {

    private final AppendixService appendixService;
    private final FileService fileService;
    private final ComFileRelManager comFileRelManager;
    @Transactional
    @Override
    public void save(Case caseEntity, List<EvidenceVO> evidenceVOList) {
        if (CollectionUtil.isEmpty(evidenceVOList)) {
            //未上传证据分组图片
            return;
        }
        Long caseId = caseEntity.getId();
        String caseType = caseEntity.getCaseType();
        String bizType = caseEntity.getBizType();
        List<AppendixSaveVO> appendixSaveVOList = new ArrayList<>();
        for (int i = 0; i < evidenceVOList.size(); i++) {
            //1、保存案件和证据分组关联关系
            EvidenceVO evidenceVO = evidenceVOList.get(i);
            String componentName = evidenceVO.getComponentName();
            EvidenceCaseRel evidenceCaseRel = new EvidenceCaseRel();
            evidenceCaseRel.setCaseId(caseId);
            evidenceCaseRel.setCaseType(caseType);
            evidenceCaseRel.setBizType(bizType);
            evidenceCaseRel.setName(StringUtils.isBlank(componentName) ? "" : componentName);
            evidenceCaseRel.setSortValue(i);
            this.superManager.save(evidenceCaseRel);
            //2、保存证据文件
            List<Long> fileIdList = evidenceVO.getFileIdList();
            if (CollectionUtil.isNotEmpty(fileIdList)) {
                for (int j = 0; j < fileIdList.size(); j++) {
                    Long fileId = fileIdList.get(j);
                    File file = fileService.getById(fileId);
                    if (BizConstant.SUFFIX_PDF.equalsIgnoreCase(file.getSuffix())) {
                        ComFileRel comFileRel = comFileRelManager.getByFileId(fileId);
                        ArgumentAssert.notNull(comFileRel, "pdf文档未拆分成图片无法保存，请重新上传");
                    }
                    appendixSaveVOList.add(AppendixSaveVO.build(caseId, AppendixType.Complaint.COMPLAINT__EVIDENCE__FILE,
                            fileId, j, null, String.valueOf(evidenceCaseRel.getId())));
                }
            }
        }
        if (CollectionUtil.isNotEmpty(appendixSaveVOList)) {
            appendixService.save(appendixSaveVOList);
        }
    }

    @Transactional
    @Override
    public void update(Case caseEntity, List<EvidenceVO> evidenceVOList) {
        Long caseId = caseEntity.getId();
        //删除案件分组和相关图片
        this.superManager.deleteByCaseId(caseId, ContextUtil.getTenantId());
        appendixService.removeByBizId(Lists.newArrayList(caseId), AppendixType.Complaint.COMPLAINT__EVIDENCE__FILE);
        this.save(caseEntity, evidenceVOList);
    }

    @Override
    public List<EvidenceCaseRelResultVO> getResultByCaseId(Long caseId, String bizType, String caseType) {
        List<EvidenceCaseRel> evidenceCaseRels = superManager.listByCaseId(caseId, bizType, caseType);
        if (CollectionUtil.isNotEmpty(evidenceCaseRels)) {
            return BeanUtil.copyToList(evidenceCaseRels, EvidenceCaseRelResultVO.class);
        }
        return Lists.newArrayList();
    }

    @Override
    public void deleteByCaseIds(List<Long> caseIds) {
        this.superManager.deleteByCaseIds(caseIds);
    }

    @Override
    public boolean updateSummarize(List<EvidenceCaseRelUpdateSummarizeVO> evidenceCaseRelUpdateSummarizeVOList) {
        evidenceCaseRelUpdateSummarizeVOList.forEach(summarizeVO -> {
            EvidenceCaseRel evidenceCaseRel = this.getById(summarizeVO.getEvidenceCaseRelId());
            if (Objects.nonNull(evidenceCaseRel)) {
                EvidenceCaseRel updateCaseRel = new EvidenceCaseRel();
                updateCaseRel.setId(evidenceCaseRel.getId());
                updateCaseRel.setSummarizeInfo(summarizeVO.getSummarizeInfo());
                updateCaseRel.setName(summarizeVO.getComponentName());
                this.superManager.updateById(updateCaseRel);
            }
        });
        return true;
    }

    @Override
    public boolean updateSummarize(EvidenceCaseRelUpdateSummarizeVO summarizeVO) {
        EvidenceCaseRel evidenceCaseRel = this.getById(summarizeVO.getEvidenceCaseRelId());
        if (Objects.nonNull(evidenceCaseRel)) {
            EvidenceCaseRel updateCaseRel = new EvidenceCaseRel();
            updateCaseRel.setId(evidenceCaseRel.getId());
            updateCaseRel.setSummarizeInfo(summarizeVO.getSummarizeInfo());
            return this.superManager.updateById(updateCaseRel);
        }
        return true;
    }
}


